package com.flycms.utils;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 转换工具类
 */
public class ConvertUtils {
    private static Logger logger = LoggerFactory.getLogger(ConvertUtils.class);

    private static Splitter splitter = Splitter.on(",").trimResults().omitEmptyStrings();
    private static Joiner joiner = Joiner.on(",").skipNulls();

    public static <T> List<T> split2List(String str, Class<T> clazz) {
        return Optional.ofNullable(str)
                .map(s -> Lists.newArrayList(splitter.split(s)).stream().map(i -> JSON.parseObject(JSON.toJSONString(i), clazz)).collect(Collectors.toList()))
                .orElseGet(Lists::newArrayList);
    }

    public static <T> String join(List<T> list) {
        if (CollectionUtils.isEmpty(list)) {
            return "";
        }
        return joiner.join(list);
    }

    public static <T> T sourceToTarget(Object source, Class<T> target) {
        if (source == null) {
            return null;
        }
        T targetObject = null;
        try {
            targetObject = target.newInstance();
            BeanUtils.copyProperties(source, targetObject);
        } catch (Exception e) {
            logger.error("convert error ", e);
        }

        return targetObject;
    }

    public static <T> List<T> sourceToTarget(Collection<?> sourceList, Class<T> target) {
        if (sourceList == null) {
            return null;
        }

        List<T> targetList = new ArrayList<>(sourceList.size());
        try {
            for (Object source : sourceList) {
                T targetObject = target.newInstance();
                BeanUtils.copyProperties(source, targetObject);
                targetList.add(targetObject);
            }
        } catch (Exception e) {
            logger.error("convert error ", e);
        }

        return targetList;
    }


    public static <T> T sourceToTarget(Object source, Class<T> target, String... ignoreProperties) {
        if (source == null) {
            return null;
        }
        T targetObject = null;
        try {
            targetObject = target.newInstance();
            BeanUtils.copyProperties(source, targetObject, ignoreProperties);
        } catch (Exception e) {
            logger.error("convert error ", e);
        }

        return targetObject;
    }

    public static <T> List<T> sourceToTarget(Collection<?> sourceList, Class<T> target, String... ignoreProperties) {
        if (sourceList == null) {
            return null;
        }

        List targetList = new ArrayList<>(sourceList.size());
        try {
            for (Object source : sourceList) {
                T targetObject = target.newInstance();
                BeanUtils.copyProperties(source, targetObject, ignoreProperties);
                targetList.add(targetObject);
            }
        } catch (Exception e) {
            logger.error("convert error ", e);
        }

        return targetList;
    }

    /**
     * 同Lists.transform但把lazy transform的行为关闭
     */
    public static <F, T> List<T> transform(Collection<F> fromList, Function<? super F, ? extends T> function) {
        if (fromList == null || fromList.size() == 0) {
            return Lists.newArrayList();
        }
        Collection<? extends T> output = Collections2.transform(fromList, function::apply);
        return Lists.newArrayList(output);
    }


}